import { TjTableTitleTips } from '@/tjComponents';
import type { showChartObjType } from './components/DimensionCharts';
import type { extendColumnsType } from '@/utils/buildTableByDimension';
import { CheckCircleOutlined, CloseCircleOutlined } from '@ant-design/icons';

const orderOptions: string[] = ['交易单元', '日期', '时点'];

const operators = {
  算法: 'diff-append',
  下发状态: 'diff-append',
  综合扣费收入偏差: 'sum',
  综合扣费电价偏差: '调节后-综合扣费电价 - 调节前-综合扣费电价',
  中长期费用偏差: 'sum',
  现货收入偏差: 'sum',
  回收费用偏差: 'sum',
  考核费用偏差: 'sum',
  实时电价: '实时-调节后-实时结算收入 / 实时-调节后-实时结算电量',
  日前电价: '日前-调节后-日前结算收入 / 日前-调节后-日前结算电量',
  合约电价: '中长期-合约收入 / 中长期-合约电量',
  综合电量: 'sum',
  '调节前-综合扣费收入': 'sum',
  '调节前-综合扣费电价': '调节前-综合扣费收入 / 综合电量',
  '调节后-综合扣费收入': 'sum',
  '调节后-综合扣费电价': '调节后-综合扣费收入 / 综合电量',
  '中长期-合约电量': 'sum',
  '中长期-合约收入': 'sum',
  '中长期-调节前-基数电量': 'sum',
  '中长期-调节前-中长期收入': 'sum',
  '中长期-调节后-基数电量': 'sum',
  '中长期-调节后-中长期收入': 'sum',
  '日前-调节前-日前申报电量': 'sum',
  '日前-调节前-日前结算电量': 'sum',
  '日前-调节前-日前结算收入': 'sum',
  '日前-调节后-日前申报电量': 'sum',
  '日前-调节后-日前结算电量': 'sum',
  '日前-调节后-日前结算收入': 'sum',
  '实时-上网电量': 'sum',
  '实时-调节前-实时结算电量': 'sum',
  '实时-调节前-实时结算收入': 'sum',
  '实时-调节后-实时结算电量': 'sum',
  '实时-调节后-实时结算收入': 'sum',
  '偏差回收-调节前-超额获利回收': 'sum',
  '偏差回收-调节前-中长期超额回收': 'sum',
  '偏差回收-调节后-超额获利回收': 'sum',
  '偏差回收-调节后-中长期超额回收': 'sum',
  '考核费用-调节前-预测精度考核': 'sum',
  '考核费用-调节前-绝对误差考核': 'sum',
  '考核费用-调节后-预测精度考核': 'sum',
  '考核费用-调节后-绝对误差考核': 'sum',
};

const other: extendColumnsType[] = [
  {
    children: [
      {
        features: { sortable: true },
        name: '综合扣费收入偏差',
        code: 'deviationOfComprehensiveDeductionIncome',
        width: 150,
        align: 'center',
        precise: 2,
      },
      {
        features: { sortable: true },
        name: '综合扣费电价偏差',
        align: 'center',
        width: 150,
        code: 'deviationOfComprehensiveDeductionPrice',
        precise: 2,
      },
      {
        features: { sortable: true },
        name: '中长期费用偏差',
        width: 150,
        align: 'center',
        code: 'mediumAndLongTermCostDeviation',
        precise: 2,
      },
      {
        features: { sortable: true },
        name: '现货收入偏差',
        width: 150,
        align: 'center',
        code: 'spotRevenueDeviation',
        precise: 2,
      },
      {
        features: { sortable: true },
        name: '回收费用偏差',
        width: 150,
        align: 'center',
        code: 'recoveryCostDeviation',
        precise: 2,
      },
      {
        features: { sortable: true },
        name: '考核费用偏差',
        width: 150,
        align: 'center',
        code: 'assessmentCostDeviation',
        precise: 2,
      },
    ],
    name: '偏差信息',
    align: 'center',
    title: (
      <TjTableTitleTips
        title="偏差信息"
        tips="显示调节前后关键指标的差异，各项的计算逻辑均为“调节后-调节前”。电量单位：MWh，电价单位：元/MWh，电费单位：元"
      />
    ),
    code: 'pcxx',
  },
  {
    children: [
      {
        features: { sortable: true },
        name: '实时电价',
        width: 140,
        align: 'center',
        code: 'spotPrice',
        precise: 2,
      },
      {
        features: { sortable: true },
        name: '日前电价',
        width: 140,
        align: 'center',
        code: 'dayAheadTariff',
        precise: 2,
      },
      {
        features: { sortable: true },
        name: '合约电价',
        width: 140,
        align: 'center',
        code: 'contractPrice',
        precise: 2,
      },
    ],
    title: (
      <TjTableTitleTips
        title="电价信息"
        tips="展示市场化中长期、现货日前、现货实时的结算价格，以上价格被调节前后共同引用。电价单位：元/MWh"
      />
    ),
    name: '电价信息',
    align: 'center',
    code: 'djxx',
  },
  {
    children: [
      {
        features: { sortable: true },
        name: '综合电量',
        width: 140,
        align: 'center',
        code: 'comprehensiveElectricity',
        precise: 3,
      },
      {
        children: [
          {
            features: { sortable: true },
            name: '综合扣费收入',
            width: 140,
            align: 'center',
            code: 'before_comprehensiveDeductionIncome',
            precise: 2,
          },
          {
            features: { sortable: true },
            name: '综合扣费电价',
            width: 140,
            align: 'center',
            code: 'before_comprehensiveDeductionTariff',
            precise: 2,
          },
        ],

        name: '调节前',
        align: 'center',
        code: 'zhBefore',
      },
      {
        children: [
          {
            features: { sortable: true },
            name: '综合扣费收入',
            width: 140,
            align: 'center',
            code: 'after_comprehensiveDeductionIncome',
            precise: 2,
          },
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '综合扣费电价',
            code: 'after_comprehensiveDeductionTariff',
            precise: 2,
          },
        ],
        name: '调节后',
        align: 'center',
        code: 'zhAfter',
      },
    ],
    align: 'center',
    name: '综合',
    title: (
      <TjTableTitleTips
        title="综合"
        tips="本项为全部收入项与支出项共同计算后的总值，即“市场化中长期+基数+现货日前市场+现货实时市场-回收-考核”。电量单位：MWh，电价单位：元/MWh，电费单位：元"
      />
    ),
    code: 'zh',
  },
  {
    children: [
      {
        features: { sortable: true },
        name: '合约电量',
        width: 140,
        align: 'center',
        code: 'mediumAndLongTerm_contractElectricity',
        precise: 3,
      },
      {
        features: { sortable: true },
        name: '合约收入',
        width: 140,
        align: 'center',
        code: 'mediumAndLongTerm_contractIncome',
        precise: 2,
      },
      {
        children: [
          {
            features: { sortable: true },
            name: '基数电量',
            width: 140,
            align: 'center',
            code: 'mediumAndLongTerm_before_baseElectricQuantity',
            precise: 3,
          },
          {
            features: { sortable: true },
            name: '中长期收入',
            width: 140,
            align: 'center',
            code: 'mediumAndLongTerm_before_mediumAndLongTermIncome',
            precise: 2,
          },
        ],

        name: '调节前',
        align: 'center',
        code: 'zcqscBefore',
      },
      {
        children: [
          {
            features: { sortable: true },
            name: '基数电量',
            width: 140,
            align: 'center',
            code: 'mediumAndLongTerm_after_baseElectricQuantity',
            precise: 3,
          },
          {
            features: { sortable: true },
            name: '中长期收入',
            width: 140,
            align: 'center',
            code: 'mediumAndLongTerm_after_mediumAndLongTermIncome',
            precise: 2,
          },
        ],
        name: '调节后',
        align: 'center',
        code: 'zcqscAfter',
      },
    ],
    title: (
      <TjTableTitleTips
        title="中长期市场"
        tips="显示中长期市场调节前后的收益数据对比，包括基数合约信息和市场化中长期交易信息。电量单位：MWh，电价单位：元/MWh，电费单位：元"
      />
    ),
    align: 'center',
    name: '中长期市场',
    code: 'zcqsc',
  },
  {
    children: [
      {
        children: [
          {
            features: { sortable: true },
            name: '日前申报电量',
            width: 140,
            align: 'center',
            code: 'dayAhead_before_dailyDeclaredElectricQuantity',
            precise: 3,
          },
          {
            features: { sortable: true },
            name: '日前结算电量',
            width: 140,
            align: 'center',
            code: 'dayAhead_before_dailySettlementElectricity',
            precise: 3,
          },
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '日前结算收入',
            code: 'dayAhead_before_settlementIncomeBeforeTheDay',
            precise: 2,
          },
        ],
        name: '调节前',
        align: 'center',
        code: 'rqscBefore',
      },
      {
        children: [
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '日前申报电量',
            code: 'dayAhead_after_dailyDeclaredElectricQuantity',
            precise: 3,
          },
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '日前结算电量',
            code: 'dayAhead_after_dailySettlementElectricity',
            precise: 3,
          },
          {
            features: { sortable: true },
            name: '日前结算收入',
            width: 140,
            align: 'center',
            code: 'dayAhead_after_settlementIncomeBeforeTheDay',
            precise: 2,
          },
        ],
        align: 'center',
        name: '调节后',
        code: 'rqscAfter',
      },
    ],
    title: (
      <TjTableTitleTips
        title="日前市场"
        tips="显示日前市场的调节前后的收益数据对比，注意，日前市场收入应引用结算电量而非出清电量。电量单位：MWh，电价单位：元/MWh，电费单位：元"
      />
    ),
    align: 'center',
    name: '日前市场',
    code: 'rqsc',
  },
  {
    children: [
      {
        features: { sortable: true },
        name: '上网电量',
        width: 140,
        align: 'center',
        code: 'realTime_onlinePower',
        precise: 3,
      },
      {
        children: [
          {
            features: { sortable: true },
            name: '实时结算电量',
            width: 140,
            align: 'center',
            code: 'realTime_before_realTimeSettlementElectricity',
            precise: 3,
          },
          {
            features: { sortable: true },
            name: '实时结算收入',
            width: 140,
            align: 'center',
            code: 'realTime_before_realTimeSettlementIncome',
            precise: 2,
          },
        ],
        name: '调节前',
        align: 'center',
        code: 'ssscBefore',
      },
      {
        children: [
          {
            features: { sortable: true },
            name: '实时结算电量',
            width: 140,
            align: 'center',
            code: 'realTime_after_realTimeSettlementElectricity',
            precise: 3,
          },
          {
            features: { sortable: true },
            name: '实时结算收入',
            width: 140,
            align: 'center',
            code: 'realTime_after_realTimeSettlementIncome',
            precise: 2,
          },
        ],
        align: 'center',
        name: '调节后',
        code: 'ssscAfter',
      },
    ],
    title: (
      <TjTableTitleTips
        title="实时市场"
        tips="显示实时市场的调节前后的收益数据对比，注意，实时市场收入应引用结算电量而非出清电量。电量单位：MWh，电价单位：元/MWh，电费单位：元"
      />
    ),
    align: 'center',
    name: '实时市场',
    code: 'sssc',
  },
  {
    children: [
      {
        children: [
          {
            features: { sortable: true },
            name: '超额获利回收',
            width: 140,
            align: 'center',
            code: 'deviationRecovery_before_excessProfitRecovery',
            precise: 2,
          },
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '中长期超额回收',
            code: 'deviationRecovery_before_mediumAndLongTermExcessRecovery',
            precise: 2,
          },
        ],
        name: '调节前',
        code: 'pchsBefore',
        align: 'center',
      },
      {
        children: [
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '超额获利回收',
            code: 'deviationRecovery_after_excessProfitRecovery',
            precise: 2,
          },
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '中长期超额回收',
            code: 'deviationRecovery_after_mediumAndLongTermExcessRecovery',
            precise: 2,
          },
        ],
        align: 'center',
        name: '调节后',
        code: 'pchsAfter',
      },
    ],
    title: (
      <TjTableTitleTips
        title="偏差回收"
        tips="显示调节前后偏差回收的对比，这里的偏差回收包括“超额获利回收”和“中长期超额回收”。电量单位：MWh，电价单位：元/MWh，电费单位：元"
      />
    ),
    name: '偏差回收',
    align: 'center',
    code: 'pchs',
  },
  {
    children: [
      {
        children: [
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '预测精度考核',
            code: 'appraisalExpense_before_forecastAccuracyAppraisal',
            precise: 2,
          },
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '绝对误差考核',
            code: 'appraisalExpense_before_absoluteErrorAssessment',
            precise: 2,
          },
        ],

        name: '调节前',
        align: 'center',
        code: 'khfyBefore',
      },
      {
        children: [
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '预测精度考核',
            code: 'appraisalExpense_after_forecastAccuracyAppraisal',
            precise: 2,
          },
          {
            features: { sortable: true },
            width: 140,
            align: 'center',
            name: '绝对误差考核',
            code: 'appraisalExpense_after_absoluteErrorAssessment',
            precise: 2,
          },
        ],
        name: '调节后',
        align: 'center',
        code: 'khfyAfter',
      },
    ],
    title: (
      <TjTableTitleTips
        title="考核费用"
        tips="显示调节前后考核费用的对比，这里的考核费用包括“预测精度考核”和“绝对误差考核”（双细则考核）。电量单位：MWh，电价单位：元/MWh，电费单位：元"
      />
    ),
    align: 'center',
    name: '考核费用',
    code: 'khfy',
  },
];

const columns = (): extendColumnsType[] => [
  {
    name: '聚合维度',
    code: 'dimension',
    align: 'center',
    lock: true,
    width: 180,
    features: { tree: true },
  },
  {
    name: '基本信息',
    align: 'center',
    lock: true,
    children: [
      {
        name: '算法名称',
        code: 'trader',
        align: 'center',
        width: 120,
      },
      {
        name: '下发状态',
        code: 'issueStatus',
        align: 'center',
        width: 80,
        transforFn: (val) => {
          if (val.length === 1) {
            const arr = ['失败', '成功', '未校验', '未调整', '失败', '失败', '失败'];
            return arr[Number(val)] ? arr[Number(val)] : '-';
          }
          return '-';
        },
        render: (val) => {
          if (val.indexOf('失败') > -1) {
            return (
              <>
                <CloseCircleOutlined style={{ color: 'rgb(253, 79, 67)', marginRight: '4px' }} />
                {val}
              </>
            );
          }
          if (val.indexOf('成功') > -1) {
            return (
              <>
                <CheckCircleOutlined style={{ color: 'rgb(48, 212, 133)', marginRight: '4px' }} />
                {val}
              </>
            );
          }
          return val;
        },
      },
    ],
  },
  ...other,
];

const chartsShowValueConfig: Record<string, Record<string, showChartObjType>> = {
  charts1: {
    deviationOfComprehensiveDeductionIncome: {
      name: '综合扣费收入偏差',
      type: 'line',
      color: '#30D485',
    },
    mediumAndLongTermCostDeviation: {
      name: '中长期收入偏差',
      type: 'bar',
      color: '#39507C',
      stack: 'A',
    },
    spotRevenueDeviation: {
      name: '现货收入偏差',
      type: 'bar',
      color: '#446C7C',
      stack: 'A',
    },
    recoveryCostDeviation: {
      name: '回收费用偏差',
      type: 'bar',
      color: '#766321',
      stack: 'A',
    },
    assessmentCostDeviation: {
      name: '考核费用偏差',
      type: 'bar',
      color: '#6E3E38',
      stack: 'A',
    },
  },
  charts2: {
    before_comprehensiveDeductionIncome: {
      name: '调节前',
      type: 'line',
      color: '#30D485',
    },
    after_comprehensiveDeductionIncome: {
      name: '调节后',
      type: 'line',
      color: '#F5BE15',
    },
    deviationOfComprehensiveDeductionIncome: {
      name: '偏差',
      type: 'bar',
      color: '#39507C',
    },
  },
  charts3: {
    before_comprehensiveDeductionTariff: {
      name: '调节前',
      type: 'line',
      color: '#30D485',
    },
    after_comprehensiveDeductionTariff: {
      name: '调节后',
      type: 'line',
      color: '#F5BE15',
    },
    deviationOfComprehensiveDeductionPrice: {
      name: '偏差',
      type: 'bar',
      color: '#39507C',
    },
  },
};

// 直接写死的导出模版头部
const exportHeader = [
  [
    '聚合维度',
    '基本信息',
    '基本信息',
    '偏差信息',
    '偏差信息',
    '偏差信息',
    '偏差信息',
    '偏差信息',
    '偏差信息',
    '电价信息',
    '电价信息',
    '电价信息',
    '综合',
    '综合',
    '综合',
    '综合',
    '综合',
    '中长期市场',
    '中长期市场',
    '中长期市场',
    '中长期市场',
    '中长期市场',
    '中长期市场',
    '日前市场',
    '日前市场',
    '日前市场',
    '日前市场',
    '日前市场',
    '日前市场',
    '实时市场',
    '实时市场',
    '实时市场',
    '实时市场',
    '实时市场',
    '偏差回收',
    '偏差回收',
    '偏差回收',
    '偏差回收',
    '考核费用',
    '考核费用',
    '考核费用',
    '考核费用',
  ],
  [
    '聚合维度',
    '算法名称',
    '下发状态',
    '综合扣费收入偏差',
    '综合扣费电价偏差',
    '中长期费用偏差',
    '现货收入偏差',
    '回收费用偏差',
    '考核费用偏差',
    '实时电价',
    '日前电价',
    '合约电价',
    '综合电量',
    '调节前',
    '',
    '调节后',
    '',
    '合约电量',
    '合约收入',
    '调节前',
    '',
    '调节后',
    '',
    '调节前',
    '',
    '调节前',
    '调节后',
    '',
    '调节后',
    '上网电量',
    '调节前',
    '',
    '调节后',
    '',
    '调节前',
    '',
    '调节后',
    '',
    '调节前',
    '',
    '调节后',
    '',
  ],
];

// 导出的何必配置
const exportMerges = [
  // 'A1:A2',
  'B1:C1',
  'D1:I1',
  'J1:L1',
  'M1:Q1',
  'R1:W1',
  'X1:AC1',
  'AD1:AH1',
  'A3:A1',
  'B3:B2',
  'C3:C2',
  'D3:D2',
  'E3:E2',
  'F3:F2',
  'G3:G2',
  'H3:H2',
  'I3:I2',
  'J3:J2',
  'K3:K2',
  'L3:L2',
  'M3:M2',
  'N2:O2',
  'P2:Q2',
  'R3:R2',
  'S3:S2',
  'T2:U2',
  'V2:W2',
  'X2:Z2',
  'AA2:AC2',
  'AD2:AD3',
  'AE2:AF2',
  'AG2:AH2',
  'AI2:AJ2',
  'AK2:AL2',
  'AM2:AN2',
  'AO2:AP2',
];

const header = [
  '',
  '算法名称',
  '下发状态',
  '综合扣费收入偏差',
  '综合扣费电价偏差',
  '中长期费用偏差',
  '现货收入偏差',
  '回收费用偏差',
  '考核费用偏差',
  '实时电价',
  '日前电价',
  '合约电价',
  '综合电量',
  '综合扣费收入',
  '综合扣费电价',
  '综合扣费收入',
  '综合扣费电价',
  '合约电量',
  '合约收入',
  '基数电量',
  '中长期收入',
  '基数电量',
  '中长期收入',
  '日前申报电量',
  '日前结算电量',
  '日前结算收入',
  '日前申报电量',
  '日前结算电量',
  '日前结算收入',
  '上网电量',
  '实时结算电量',
  '实时结算收入',
  '实时结算电量',
  '实时结算收入',
  '超额获利回收',
  '中长期超额回收',
  '超额获利回收',
  '中长期超额回收',
  '预测精度考核',
  '绝对误差考核',
  '预测精度考核',
  '绝对误差考核',
];

export {
  orderOptions,
  operators,
  columns,
  chartsShowValueConfig,
  exportHeader,
  exportMerges,
  header,
};
